package com.freecloud.javabasics.classload;

import java.io.*;

/**
 * The interface for input stream
 *
 * Only sequential access allowed. Cannot get length information.
 *
 * Note:
 *     Ask question if anything is not clear.
 *     The implementations are just mock. Your code should not depend on them.
 */
class InStream
{
    // TASK 1: PLEASE ADD UNITTEST CASES HERE.
    //private static String UnittestCases = "^%oA i923diuo";

    private static String UnittestCases = "^%oA i923diuo\nsafdsafdsafdsafdsa\nfdsafdsafdsew22323fds\n4321fdsafdlsafjiefwaf\nfdasfdsa2342";

    public Character readChar() throws IOException
    {
        int code = implReader.read();
        if (code == -1)
            return null;

        return Character.valueOf((char)code);
    }

    public String readLine() throws IOException
    {
        return implReader.readLine();
    }

    public String readBuffer(int size) throws IOException
    {
        char[] buffer = new char[size];
        int count = implReader.read(buffer, 0, size);
        if (count == -1)
            return null;
        else
            return new String(buffer);
    }

    public InStream()
    {
        implStream = new ByteArrayInputStream(UnittestCases.getBytes());
        implReader = new BufferedReader(new InputStreamReader(implStream),6);
    }

    private ByteArrayInputStream implStream = null;
    private BufferedReader implReader = null;
}

/**
 * The interface for output stream
 *
 * Note:
 *     Ask question if anything is not clear.
 *     The implementations are just mock. Your code should not depend on them.
 */
class OutStream
{
    public void writeChar(char ch)
    {
        System.out.print(ch);
    }

    public void writeLine(String line)
    {
        System.out.println(line);
    }

    public void writeBuffer(String buff)
    {
        System.out.print(buff);
    }
}

class Processor
{
    public Processor(InStream instream, OutStream outstream)
    {
        this.instream = instream;
        this.outstream = outstream;
    }

    public void LongestValidSubStrings() throws IOException
    {
        // TASK 2: PLEASE IMPLEMENT THIS.
        // Note: You may define other functions and/or classes as you wish
        String str;
//        while ((str = instream.readLine()) != null){
//            System.out.println(str);
//        }

        while ((str = instream.readBuffer(8)) != null){
            System.out.println(str);
        }
    }

    private InStream instream = null;
    private OutStream outstream = null;
}

class ShowMeBug
{
    public static void main(String[] args)throws IOException
    {
        InStream instream = new InStream();
        OutStream outstream = new OutStream();
        Processor processor = new Processor(instream, outstream);
        processor.LongestValidSubStrings();
    }
}